﻿Imports System.Data.OleDb

Public Class GallaryAddImage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnAddImage_Click(sender As Object, e As EventArgs) Handles btnAddImage.Click

        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(FUImage.FileName)

        If FUImage.HasFile Then
            FUImage.SaveAs(Server.MapPath("Pictures") & "/" & newFileName)
        End If


        Dim sql As String = "INSERT INTO  Images (ImageLink,ImageDescription,DateUploaded,TimeUploaded,UserIDFK) VALUES (@f1,@f2,@f3,@f4,@f5)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MercedesConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f2", txtdescription.Text)
        cmd.Parameters.AddWithValue("@f3", Now.Date.ToString("dd/MM/yyyy"))
        cmd.Parameters.AddWithValue("@f4", DateTime.Now.ToString("t"))
        cmd.Parameters.AddWithValue("@f5", Membership.GetUser(User.Identity.Name).ProviderUserKey)


        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()

        Response.Redirect("Gallary.aspx")




    End Sub


    ' Make sure it does not have the same file name. 

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function
End Class